package com.njwb.day15.zqzb.jdbc03;

import com.njwb.day15.zqzb.util.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class FindStudentById {


    public static Object[] execute(int id){
        Object[] objects =null;
        //构建连接对象
        Connection connection= null ;
        //命令器
        PreparedStatement pstmt= null ;
        //结果集
        ResultSet rs = null ;
        try {
            connection = DbUtil.getConnection();
            //3、创建命令器
            pstmt = connection.prepareStatement("select sid,sname,age,address from tb_student where sid =?");
            pstmt.setInt(1,id);
            //5.执行sql语句,返回结果集对象，并读取结果集
            rs = pstmt.executeQuery();
            //读取结果集
            if(rs.next()) {
                objects = new Object[4];
                objects[0] = rs.getInt("sid");
                objects[1]=rs.getString("sname");
                objects[2] = rs.getInt("age");
                objects[3] = rs.getString("address");
            }
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DbUtil.closeConnection(rs,pstmt,connection);
        }
        return objects ;
    }
}
